package com.fenghongzhang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.fenghongzhang.day008.R;

public class LoginActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private CheckBox cbRemember;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        cbRemember = (CheckBox) findViewById(R.id.cb_remember);
        login = (Button) findViewById(R.id.login);


        //第一次启动软件的时候,输入框有没有值,根据多选框是否被选中来判断
        SharedPreferences login1 = getSharedPreferences("login", MODE_PRIVATE);
        boolean isLogin = login1.getBoolean("isLogin", false);

        if(isLogin){
            String name = login1.getString("name", "");
            String pwd = login1.getString("pwd", "");
            username.setText(name);
            password.setText(pwd);
            cbRemember.setChecked(true);
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences login = getSharedPreferences("login", MODE_PRIVATE);
                SharedPreferences.Editor edit = login.edit();
                //看看多选框的状态
                boolean checked = cbRemember.isChecked();
                if(checked){
                    String name = username.getText().toString();
                    String pwd = password.getText().toString();
                    edit.putString("name",name);
                    edit.putString("pwd",pwd);
                }

                edit.putBoolean("isLogin",checked);
                edit.commit();
            }
        });


    }
}
